/**
 *
 */
package org.tangram.component.xml;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

/**
 * @author han_feng
 * 
 */
@Root(name = "component", strict = false)
public class ComponentDefintion {

	@Attribute
	private String modelVersion;

	@Element
	private String groupId;

	@Element
	private String id;

	@Element
	private String version;

	@Element(required = false)
	private String name;

	@Element(required = false, data = true)
	private String description = "";

	@ElementList(inline = true, name = "dependencies", entry = "dependency", empty = true, required = false)
	private List<DependencyDefintion> dependencyDefintions = new ArrayList<DependencyDefintion>();

	private URL descriptorURL;

	@ElementList(inline = true, entry = "extension-point", required = false, empty = true)
	private List<ExtensionPointDefintion> extensionPointDefintions = new ArrayList<ExtensionPointDefintion>();

	@ElementList(inline = true, entry = "extension", required = false, empty = true)
	private List<ExtensionDefintion> extensionDefintions = new ArrayList<ExtensionDefintion>();

	// 解决simple-xml 2.3.6的一个空指针异常，在当前simple-xml最新代码中已经解决，但未发布:
	// @Attribute(required = false)
	// @Namespace(prefix = "xsi", reference =
	// "http://www.w3.org/2001/XMLSchema-instance")
	// @SuppressWarnings("unused")
	// private String noNamespaceSchemaLocation;
	//
	// @Attribute(required = false)
	// @Namespace(prefix = "xsi", reference =
	// "http://www.w3.org/2001/XMLSchema-instance")
	// @SuppressWarnings("unused")
	// private String schemaLocation;

	public ComponentDefintion(@Element(name = "groupId") String groupId,
			@Element(name = "id") String id,
			@Element(name = "version") String version) {
		this.groupId = groupId;
		this.id = id;
		this.version = version;
	}

	/**
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;

		if (obj == this)
			return true;

		if (obj instanceof ComponentDefintion) {
			ComponentDefintion cd = (ComponentDefintion) obj;
			return cd.getKey().equals(this.getKey());
		}

		return super.equals(obj);
	}

	/**
	 * @return the dependencies
	 */
	public List<DependencyDefintion> getDependencyDefintions() {
		return dependencyDefintions;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @return the descriptorURL
	 */
	public URL getDescriptorURL() {
		return descriptorURL;
	}

	/**
	 * @return the extensions
	 */
	public List<ExtensionDefintion> getExtensionDefintions() {
		return extensionDefintions;
	}

	/**
	 * @return the extensionPointDefintions
	 */
	public List<ExtensionPointDefintion> getExtensionPointDefintions() {
		return extensionPointDefintions;
	}

	/**
	 * @return the groupId
	 */
	public String getGroupId() {
		return groupId;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	public String getKey() {
		return this.getGroupId() + ":" + this.getId();
	}

	/**
	 * @return the modelVersion
	 */
	public String getModelVersion() {
		return modelVersion;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the version
	 */
	public String getVersion() {
		return version;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return this.getKey().hashCode();
	}

	/**
	 * @param descriptorURL
	 *            the descriptorURL to set
	 */
	public void setDescriptorURL(URL descriptorURL) {
		this.descriptorURL = descriptorURL;
	}

	public String toString() {
		return this.getKey() + "|" + this.getDescriptorURL();
	}

}
